package com.aop.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Aspect
public class HelloAspect {

//    @Before()
//    @After()
//    @Around()
//    @AfterReturning
//    @AfterThrowing
//    @Before("execution(* com.aop.service.UserService.*(..))")
    public void aaa(JoinPoint joinPoint){
        System.out.println("调用方法:"+joinPoint.getSignature().getName());
        System.out.println("参数列表:"+ Arrays.toString(joinPoint.getArgs()));
        System.out.println("aaa");
        System.out.println("bbb");
        System.out.println("ccc");
    }

    @AfterThrowing(pointcut = "execution(* com.aop.service.UserService.*(..))",
    throwing = "ex")
    public void ccc(RuntimeException ex){
        System.out.println(System.currentTimeMillis() +":"+ ex.getMessage());
    }

//    环绕增强
//    @Around("execution(* com.aop.service.UserService.*(..))")
    public Object doAround(ProceedingJoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        System.out.println("即将调用方法:"+name);
        System.out.println("参数列表:"+ Arrays.toString(joinPoint.getArgs()));
        try {
             Object result = joinPoint.proceed();
             return result;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            throw new RuntimeException(throwable);
        }finally {
            System.out.println("完成调用方法:"+name);
        }

    }
}
